package com.example.zjx8969.myapplication.view;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

public class RadarAnimationView extends View implements View.OnClickListener {
    private Bitmap innerBitmap, outerBitmap;
    private Rect rect = new Rect();
    private Rect rectInside = new Rect();

    private final Paint DEFAULT_PAINT, shadowPaint;
    private int insideDiameter;
    private float radius = 1;
    boolean isAnimationRun = false;

    public RadarAnimationView(Context context) {
        this(context, null, 0);

    }

    public RadarAnimationView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RadarAnimationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        DEFAULT_PAINT = new Paint(Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);
        shadowPaint = new Paint();
        shadowPaint.setAntiAlias(true);

        startAnimator();


        setOnClickListener(this);

    }

    private void startAnimator() {
        new Thread() {
            @Override
            public void run() {
                while (isAnimationRun) {
                    try {
                        Thread.sleep(150);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    radius += 50;
                    if (radius >= getWidth() >> 1) radius = 100;
                    RadialGradient gradient = new RadialGradient(rect.centerX(), rect.centerY(), radius,
                            new int[]{Color.TRANSPARENT, Color.BLUE, Color.WHITE}, new float[]{0.8f, 0.9f, 1.0f}, Shader.TileMode.CLAMP);
                    shadowPaint.setShader(gradient);
                    postInvalidate();
                    Log.d("tag", radius + "");
                }


            }
        }.start();
    }


    public void reverseStatus() {
        if (isAnimationRun) {
            isAnimationRun = false;
        } else {
            isAnimationRun = true;
            startAnimator();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.d("tag", "cancel animation");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.makeMeasureSpec(widthMeasureSpec, MeasureSpec.EXACTLY);
        setMeasuredDimension(width, width);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /*canvas.drawBitmap(outerBitmap, null, rect, DEFAULT_PAINT);*/
        canvas.drawCircle(rect.centerX(), rect.centerY(), radius, shadowPaint);
        Log.d("tag", rect.toShortString() + " " + radius);
//        if (innerBitmap != null) canvas.drawBitmap(innerBitmap, null, rectInside, DEFAULT_PAINT);
    }

    private Bitmap toCircle(@NonNull Bitmap bitmap) {
        int min = Math.min(bitmap.getWidth(), bitmap.getHeight());
        Bitmap dst = Bitmap.createBitmap(min, min, bitmap.getConfig());
        Canvas canvas = new Canvas(dst);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        int radius = min >> 1;
        canvas.drawCircle(radius, radius, radius, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, 0, 0, paint);
        return dst;
    }

    public void setBitmap(Bitmap bitmap) {
        this.innerBitmap = toCircle(bitmap);
        postInvalidate();
    }

    public void setBitmap(String url) {
        Glide.with(getContext()).asBitmap().load(url).override(rectInside.width()).circleCrop().into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                innerBitmap = resource;
                postInvalidate();
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {

            }
        });
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        rect.left = rect.top = 0;
        rect.right = w;
        rect.bottom = h;

        int padding = (w - insideDiameter) >> 1;
        rectInside.left = rectInside.top = padding;
        rectInside.right = rectInside.bottom = (insideDiameter + padding);
        radius = getWidth() >> 1;

        RadialGradient gradient = new RadialGradient(rect.centerX(), rect.centerY(), radius,
                new int[]{Color.TRANSPARENT, Color.BLUE, Color.WHITE}, new float[]{0.8f, 0.9f, 1.0f}, Shader.TileMode.CLAMP);
        shadowPaint.setShader(gradient);

//        Log.d("tag", String.format("onSizeChanged:w=%d h=%d inside=%s,outSide=%s ", w, h, rectInside.toShortString(), rect.toShortString()));
    }

    @Override
    public void onClick(View v) {

    }
}
